
#include <Wire.h>

#include <Servo.h>

#define servo1_time 100
#define servo2_time 100
#define servo3_time 100
Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
char servo1 = '1';
char servo2 = '1';
char servo3 = '1';

char preservo1 = '1';
char preservo2 = '1';
char preservo3 = '1';

char servo1_test;
char servo2_test;

int servo1_count;
int servo2_count;
int servo3_count;

int servo1_flag = 0;
int servo2_flag = 0;
int servo3_flag = 0;

int Mode_Flag;
char Receive1;
char Receive2;
char Receive3;
void setup()
{
  Serial.begin(115200);
  Wire.begin(8);                // 用#1地址  连接i2c总线
  Wire.onReceive(receiveEvent); // 注册事件 receiveEvent()具体内容见最后
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(7);
  myservo3.attach(8);  // attaches the servo on pin 9 to the servo object

  myservo1.write(90);
  myservo2.write(90);
  myservo3.attach(90);
  delay(15);
}


void loop()
{


}


void receiveEvent(int howMany) {

  Mode_Flag = 0;
  int i_1 = 0;
  int i_2 = 0;
  int i_3 = 0;
  //  Serial.print("Message is ");
  while (1 < Wire.available()) { // 循环浏览 ，除了最后一个
    char c = Wire.read();        // 以字符形式接收字节
    if (c == 'x')
    {
      Mode_Flag = 1;
    }
    if (c == 'y')
    {
      Mode_Flag = 2;
    }
    if (c == 'z')
    {
      Mode_Flag = 3;
    }
    if (c == 'w')
    {
      Mode_Flag = 4;
    }
    if (c == 's')
    {
      Mode_Flag = 5;
    }
    if (c == 'd')
    {
      Mode_Flag = 6;
    }



    if (Mode_Flag == 1 && c != 'x')
    {
      Receive1 = c;
    }
    if (Mode_Flag == 2 && c != 'y')
    {
      Receive2 = c;
    }
    if (Mode_Flag == 3 && c != 'z')
    {
      Receive3 = c;
    }
    if (Mode_Flag == 4 && c != 'w')
    {
      servo1 = c;
    }
    if (Mode_Flag == 5 && c != 's')
    {
      servo2 = c;
    }
    if (Mode_Flag == 6 && c != 'd')
    {
      servo3 = c;
    }


  }
  int x = Wire.read();
  if (preservo1 == '0' && servo1 == '1')
  {
    servo1_flag = 1;
  }
  if (preservo2 == '0' && servo2 == '1')
  {
    servo2_flag = 1;
  }
  if (preservo3 == '0' && servo3 == '1')
  {
    servo3_flag = 1;
  }


  Serial.print(servo1_flag);             // 打印出来
  Serial.print(servo2_flag);
  Serial.println(servo3_flag);
  preservo1 = servo1;
  preservo2 = servo2;
  preservo3 = servo3;

  if (servo1_flag == 1 && servo1_count <= servo1_time)
  {
    myservo1.write(10);
    Serial.println("servo1 is on");
    //    delay(1000);
    servo1_count++;
  }
  else
  {
    myservo1.write(90);
  }
  if (servo1_count > servo1_time)
  {
    servo1_count = 0;
    servo1_flag = 0;
  }

  if (servo2_flag == 1 && servo2_count <= servo2_time)
  {
    myservo2.write(180);
    Serial.println("servo2 is on");
    //    delay(1000);
    servo2_count++;
  }
  else
  {
    myservo2.write(90);
  }
  if (servo2_count > servo2_time)
  {
    servo2_count = 0;
    servo2_flag = 0;
  }


  if (servo3_flag == 1 && servo3_count <= servo3_time)
  {
    myservo3.write(180);
    Serial.println("servo3 is on");
    //    delay(1000);
    servo3_count++;
  }
  else
  {
    myservo3.write(90);
  }
  if (servo3_count > servo3_time)
  {
    servo3_count = 0;
    servo3_flag = 0;
  }

}
